package 力扣._00周赛._336周赛._8028_执行操作使两个字符串相等;

import java.util.ArrayList;
import java.util.List;

public class Solution {


    public int minOperations(String s1, String s2, int x) {
        //相差的是偶数个则可以，奇数个则不行
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) != s2.charAt(i))
                list.add(i);
        }
        if (list.size() % 2 == 1)
            return -1;
        if (list.size() == 0)
            return 0;
        int cost = 0;
        //将不连续的在代价小于x的前提下用第一种尝试使其连续
        //统一向左边靠
        for (int i = 1; i < list.size() - 1; i++) {
            if (list.get(i-1) + 1 != list.get(i)) {
                int d = list.get(i) - list.get(i-1) - 1;    //需要d次第一种，才能使其连续
                if (d + 1 <= x) {
                    cost += d;
                    list.set(i, list.get(i-1) +1);
                }
            }
        }
        //将靠在一起的用第一种翻转

        for (int i = list.size() - 2; i >= 0; i--) {
            if (list.get(i) + 1 == list.get(i + 1)) {
                cost += 1;
                list.remove(list.get(i + 1));
                list.remove(list.get(i));
            }
        }
        cost += list.size() / 2 * x;
        return cost;
    }


    public static void main(String[] args) {
        int i = new Solution().minOperations("101101", "000000", 6);
        System.out.println(i);
    }
}
